package com.meixiang.adapter.shopping;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.PaymentBean;
import com.meixiang.recyclerview.OnItemClick;
import com.meixiang.tool.Tool;
import com.meixiang.util.GlideHelper;
import com.meixiang.view.LineView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 美享收银台列表适配器
 *
 * @author LZRUI
 */
public class PayPlatformAdapter extends RecyclerView.Adapter<PayPlatformAdapter.PlatformViewHolder> {

    private Context mContext;
    private List<PaymentBean> mDatas;
    private OnItemClick onItemClick;

    public void setOnItemClick(OnItemClick itemClick) {
        this.onItemClick = itemClick;
    }

    public PayPlatformAdapter(Context context) {
        this.mContext = context;
    }

    public void refreshData(List<PaymentBean> datas) {
        if (datas != null) {
            this.mDatas = datas;
            notifyDataSetChanged();
        }
    }

    @Override
    public PlatformViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_pay_platform, parent, false);
        return new PlatformViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final PlatformViewHolder holder, final int position) {

        PaymentBean entity = mDatas.get(position);
        if (entity == null) return;
        holder.tvPlatformName.setText(entity.getPaymentName());
        GlideHelper.showImage(mContext, entity.getPaymentLogo(), R.mipmap.pay_pl_meixiang, holder.ivPlatformLogo);
        holder.tvPlatformHint.setText(entity.getPaymentDesc());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Tool.isFastDoubleClick()) return;
                if (onItemClick != null) {
                    onItemClick.onItemClick(v, holder.getLayoutPosition());
                }
            }
        });

        if (position != mDatas.size() - 1) {
            // 不是最后一个item分割线需要左边距
            holder.line.setMarginLeft(15);
        }
    }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    static class PlatformViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.iv_platform_logo)
        ImageView ivPlatformLogo;
        @Bind(R.id.tv_platform_name)
        TextView tvPlatformName;
        @Bind(R.id.tv_platform_hint)
        TextView tvPlatformHint;
        @Bind(R.id.line)
        LineView line;

        public PlatformViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
